/**
 * Created by jinsebainian on 2017/9/22.
 */
var config = require('./dbHelper');
var mysql  = require('mysql');
var UserDao = function(){
    this.login=function (user,callback) {
        var helper= mysql.createConnection(config);
        helper.connect();
        var result={
            success:false,
            data:null,
            info:''
        };
        var sql = "SELECT * FROM user where userName = '"+user.getUserName()+"' and password = '"+user.getPassword()+"'";
        helper.query(sql,function (err, resultData) {
            if (err) {
                result.success = false;
                result.info = '查询出错'
                callback(result);
            }
            if (resultData.length > 0) {
                result.success = true;
                result.data = resultData;
                result.info = '查询成功'
            }else{
                result.success = false;
                result.info = '用户不存在'
            }
            callback(result);
        });
        helper.end();
    }

    this.regist=function (user,callback) {
        var helper= mysql.createConnection(config);
        helper.connect();
        var result={
            success:false,
            data:null,
            info:''
        };
        var sql = "insert into user(userName,password) values(?,?)";
        var params = [user.getUserName(), user.getPassword()];
        helper.query(sql,params,function (err, data) {
            if (err) {
                result.success = false;
                result.info = '注册出错'
                callback(result);
            }
            console.log("---data:"+data)
            if (data) {
                result.success = true;
                result.data = data;
                result.info = '注册成功'
            }else{
                result.success = false;
                result.info = '收拾收拾'
            }
            callback(result);
        });
        helper.end();
    }
}
module.exports = UserDao;